<?php
/**
 * @author Zwc
 * Created On 2021/5/3 17:18
 */

namespace app\auth\service;


use app\common\models\ConfigModel;

class ConfigService extends BaseService
{
    protected $model;
    public function __construct()
    {
        $this->model = new ConfigModel();
        parent::__construct();
    }

    /**
     * 获取配置信息
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getData() {
        $condition = [];
        $data = $this->model->where($condition)->order('id asc')->select();
        return $data;
    }

    /**
     * 添加配置
     * @param $params
     */
    public function addConfig($params) {
        $condition = [
            'type' => $params['type']
        ];
        $count = $this->model->where($condition)->count();
        if($count > 0) {
            return ['code' => 99,'msg' => '配置类型重复'];
        }
        $res = $this->model->insertGetId($params);
        if($res > 0) {
            return ['code' => 0,'msg' => '添加成功','data' => $res];
        }else {
            return ['code' => 1,'msg' => '添加失败'];
        }
    }

    public function modifyConfig($params) {
        $condition = [
            ['type','=',$params['type']],
            ['id','<>',$params['id']]
        ];
        $count = $this->model->where($condition)->count();
        if($count > 0) {
            return ['code' => 99,'msg' => '配置类型重复'];
        }
        $res = $this->model->where(['id' => $params['id']])->save($params);
        if($res !== false) {
            return ['code' => 0,'msg' => '操作成功'];
        }else {
            return ['code' => 1,'msg' => '操作失败'];
        }
    }

    /**
     * 详情
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($id) {
        $data = $this->model->where(['id' => $id])->find();
        return $data;
    }



}